﻿// --------------------------------
// <copyright file="ListPropertyGrid.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>25. 11. 2011 16:05:40</date>

// <summary>
// 
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using FITClub.LevelBuilder.Helper;

namespace FITClub.LevelBuilder
{
    public partial class ListPropertyGrid : UserControl
    {
        public event Action<Type> AddRequest;
        public event Action<Type, IList> RemoveRequest;
        public event Action<ListPropertyGrid> SelectionChanged;
 
        private IList dataSource;

        public IList DataSource
        {
            get { return dataSource; }
            set
            {
                this.dataSource = value;
                InvalidateDataSource();
            }
        }

        public bool ToolbarVisible
        {
            get { return this.toolStrip1.Visible; }
            set { this.toolStrip1.Visible = value; }
        }

        public ListPropertyGrid()
        {
            InitializeComponent();

        }

        public IEnumerable<object> SelectedItems
        {
            get { return this.listBox1.SelectedItems.Cast<object>(); }
        }

        public void InvalidateDataSource()
        {
            if (dataSource != null)
            {
                this.listBox1.DataSource = null;
                this.listBox1.DataSource = dataSource;
            }
            else
            {
                int selIndex = this.listBox1.SelectedIndex;
                this.listBox1.DataSource = null;
                this.propertyGrid2.SelectedObject = null;
                if (selIndex >= 0 && selIndex < this.listBox1.Items.Count)
                {
                    this.listBox1.SelectedIndex = selIndex;
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItems.Count > 1)
            {
                object[] items = new object[listBox1.SelectedItems.Count];
                listBox1.SelectedItems.CopyTo(items, 0);
                this.propertyGrid2.SelectedObjects = items;
            }
            else
            {
                this.propertyGrid2.SelectedObject = listBox1.SelectedItem;
            }

            if (this.SelectionChanged != null)
            {
                this.SelectionChanged(this);
            }
        }

        private void propertyGrid2_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            InvalidateDataSource();
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            if (this.dataSource != null && this.AddRequest != null)
            {
                this.AddRequest(this.dataSource.GetType());
                InvalidateDataSource();
            }
        }

        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            if (this.dataSource != null && this.RemoveRequest != null && this.listBox1.SelectedItems.Count > 0)
            {
                object[] array = new object[this.listBox1.SelectedItems.Count];
                this.listBox1.SelectedItems.CopyTo(array, 0);
                this.RemoveRequest(this.dataSource.GetType(), array);
                InvalidateDataSource();
            }
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InvalidateDataSource();
        }

        private void listBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                this.removeToolStripMenuItem.PerformClick();
                return;
            }

            if (e.Control)
            {
                if ((e.KeyData & Keys.A) == Keys.A)
                {
                    for (int i = 0; i < this.listBox1.Items.Count; i++)
                    {
                        this.listBox1.SelectedIndices.Add(i);
                    }
                    return;
                }
                
                if ((e.KeyData & Keys.N) == Keys.N)
                {
                    this.addToolStripMenuItem.PerformClick();
                    return;
                }

                if ((e.KeyData & Keys.R) == Keys.R)
                {
                    this.updateToolStripMenuItem.PerformClick();
                    return;
                }
            }
        }
    }
}
